package com.ysm.smfu.fragment;

import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.WriterException;
import com.ysm.smfu.R;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.utils.MyImageBitmapUtil;
import com.ysm.smfu.utils.PrintUtil;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.welcome.SharedConfig;
import com.ysm.smfu.zxing.encoding.EncodingHandler;
import com.zhy.autolayout.AutoLinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/9/26.
 */
public class QrCodeFragment extends MyBaseFragment {

    @BindView(R.id.fagment_my_qrcode_img)
    ImageView fagmentMyQrcodeImg;
    @BindView(R.id.addF)
    TextView addF;
    @BindView(R.id.saveToLoc)
    AutoLinearLayout saveToLoc;
    private String name;
    private String invitUrl;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = UIUtils.inflate(R.layout.fragmeng_my_qrcode);
        ButterKnife.bind(this, view);
        SharedPreferences sp = new SharedConfig(UIUtils.getContext()).getCommoneSp();
        name = sp.getString(SpKey.NAME, "");
        SharedPreferences currShared = new SharedConfig(UIUtils.getContext(), name).getPrivateSp();
        String userId = currShared.getString(SpKey.USERID, "");
        Bundle arguments = getArguments();
        if (arguments != null) {
            invitUrl = arguments.getString(SpKey.INVITURL);
        }
        if (!TextUtils.isEmpty(invitUrl)) {
            addF.setVisibility(View.GONE);
            saveToLoc.setVisibility(View.VISIBLE);
        }

        saveToLoc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyImageBitmapUtil.saveQrToLoc(getActivity(), qrCodeBitmap);

            }
        });
        createQRCode(userId);
        return view;
    }

    Bitmap qrCodeBitmap = null;

    /**
     * 生成二维码
     *
     * @param userId
     */
    private void createQRCode(String userId) {

//        Bitmap logo = MyImageBitmapUtil.getBitmapLoc(name);
//        个人二维码
        if (TextUtils.isEmpty(invitUrl)) {
            //根据字符串生成二维码图片并显示在界面上，第二个参数为图片的大小（350*350）
            try {
                qrCodeBitmap = EncodingHandler.createQRCode(SpKey.QRADDFRIEND + userId, 468, 468, null);
            } catch (WriterException e) {

            }
        } else {
//            邀请好友二维码
            try {
                qrCodeBitmap = EncodingHandler.createQRCode(invitUrl, 468, 468, null);
                PrintUtil.printRespones("二维码信息" ,invitUrl);
            } catch (WriterException e) {

            }
        }
        fagmentMyQrcodeImg.setImageBitmap(qrCodeBitmap);
    }
}
